//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.visualdata.controller;

import ciicos.base.ActionResult;
import ciicos.base.controller.SuperController;
import ciicos.util.JsonUtil;
import ciicos.util.StringUtil;
import ciicos.visualdata.entity.VisualDbEntity;
import ciicos.visualdata.model.VisualPageVO;
import ciicos.visualdata.model.VisualPagination;
import ciicos.visualdata.model.visualdb.VisualDbCrForm;
import ciicos.visualdata.model.visualdb.VisualDbInfoVO;
import ciicos.visualdata.model.visualdb.VisualDbListVO;
import ciicos.visualdata.model.visualdb.VisualDbQueryForm;
import ciicos.visualdata.model.visualdb.VisualDbSelectVO;
import ciicos.visualdata.model.visualdb.VisualDbUpForm;
import ciicos.visualdata.service.VisualDbService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(
        name = "大屏数据源配置",
        description = "db"
)
@RequestMapping({"/api/blade-visual/db"})
public class VisualDbController extends SuperController<VisualDbService, VisualDbEntity> {
    @Autowired
    private VisualDbService dbService;

    @Operation(
            summary = "分页"
    )
    @GetMapping({"/list"})
    public ActionResult<VisualPageVO<VisualDbListVO>> list(VisualPagination pagination) {
        List<VisualDbEntity> data = this.dbService.getList(pagination);
        List<VisualDbListVO> list = JsonUtil.getJsonToList(data, VisualDbListVO.class);
        VisualPageVO paginationVO = (VisualPageVO)JsonUtil.getJsonToBean(pagination, VisualPageVO.class);
        paginationVO.setRecords(list);
        return ActionResult.success(paginationVO);
    }

    @Operation(
            summary = "详情"
    )
    @GetMapping({"/detail"})
    @Parameters({@Parameter(
            name = "id",
            description = "主键",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult<VisualDbInfoVO> info(@RequestParam("id") String id) {
        VisualDbEntity entity = this.dbService.getInfo(id);
        VisualDbInfoVO vo = (VisualDbInfoVO)JsonUtil.getJsonToBean(entity, VisualDbInfoVO.class);
        return ActionResult.success(vo);
    }

    @Operation(
            summary = "新增或修改"
    )
    @PostMapping({"/submit"})
    @Parameters({@Parameter(
            name = "dbUpForm",
            description = "数据模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult submit(@RequestBody VisualDbUpForm dbUpForm) {
        VisualDbEntity entity = (VisualDbEntity)JsonUtil.getJsonToBean(dbUpForm, VisualDbEntity.class);
        if (StringUtil.isEmpty(entity.getId())) {
            this.dbService.create(entity);
            return ActionResult.success("新建成功");
        } else {
            this.dbService.update(entity.getId(), entity);
            return ActionResult.success("更新成功");
        }
    }

    @Operation(
            summary = "新增"
    )
    @PostMapping({"/save"})
    @Parameters({@Parameter(
            name = "dbCrForm",
            description = "数据模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult create(@RequestBody VisualDbCrForm dbCrForm) {
        VisualDbEntity entity = (VisualDbEntity)JsonUtil.getJsonToBean(dbCrForm, VisualDbEntity.class);
        this.dbService.create(entity);
        return ActionResult.success("新建成功");
    }

    @Operation(
            summary = "修改"
    )
    @PostMapping({"/update"})
    @Parameters({@Parameter(
            name = "dbUpForm",
            description = "数据模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult update(@RequestBody VisualDbUpForm dbUpForm) {
        VisualDbEntity entity = (VisualDbEntity)JsonUtil.getJsonToBean(dbUpForm, VisualDbEntity.class);
        this.dbService.update(entity.getId(), entity);
        return ActionResult.success("更新成功");
    }

    @Operation(
            summary = "删除"
    )
    @PostMapping({"/remove"})
    @Parameters({@Parameter(
            name = "ids",
            description = "主键",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult delete(String ids) {
        VisualDbEntity entity = this.dbService.getInfo(ids);
        if (entity != null) {
            this.dbService.delete(entity);
            return ActionResult.success("删除成功");
        } else {
            return ActionResult.fail("删除失败，数据不存在");
        }
    }

    @Operation(
            summary = "下拉数据源"
    )
    @GetMapping({"/db-list"})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult<List<VisualDbSelectVO>> list() {
        List<VisualDbEntity> data = this.dbService.getList();
        List<VisualDbSelectVO> list = JsonUtil.getJsonToList(data, VisualDbSelectVO.class);
        return ActionResult.success(list);
    }

    @Operation(
            summary = "数据源测试连接"
    )
    @PostMapping({"/db-test"})
    @Parameters({@Parameter(
            name = "dbCrForm",
            description = "数据源模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult test(@RequestBody VisualDbCrForm dbCrForm) {
        VisualDbEntity entity = (VisualDbEntity)JsonUtil.getJsonToBean(dbCrForm, VisualDbEntity.class);
        boolean flag = this.dbService.dbTest(entity);
        return flag ? ActionResult.success("连接成功") : ActionResult.fail("连接失败");
    }

    @Operation(
            summary = "动态执行SQL"
    )
    @PostMapping({"/dynamic-query"})
    @Parameters({@Parameter(
            name = "queryForm",
            description = "数据模型",
            required = true
    )})
    public ActionResult query(@RequestBody VisualDbQueryForm queryForm) {
        VisualDbEntity entity = this.dbService.getInfo(queryForm.getId());
        List<Map<String, Object>> data = new ArrayList();
        if (entity != null) {
            data = this.dbService.query(entity, queryForm.getSql());
        }

        return ActionResult.success(data);
    }
}
